/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package jbotsim.ui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import jbotsim.Link;
import jbotsim.Node;
import jbotsim.Topology;
import jbotsim.event.ConnectivityListener;
import jbotsim.event.MovementListener;
import jbotsim.event.PropertyListener;
import jbotsim.event.TopologyListener;
/**
 *
 * @author vincent
 */
public class NewJPanel extends javax.swing.JPanel {
 protected Vector<ActionListener> actionListeners=new Vector<ActionListener>();
    protected Vector<String> actionCommands=new Vector<String>();
    protected Topology topo;
    protected NewJPanel jtopo=this;
    protected NewJPanel.EventHandler handler=new NewJPanel.EventHandler();
	protected boolean showDrawings=true;
    
    /**
     * Creates a new JTopology for the specified topology.
     * @param topo The topology to encapsulate.
     */
    
    
    
    public void settopo(Topology topo)
    {
        this.topo=topo;
    	topo.addConnectivityListener(handler);
    	topo.addTopologyListener(handler);
    	topo.addMovementListener(handler);
        super.setLayout(null);
        super.setBackground(new Color(180,180,180));
        super.addMouseListener(handler);
        for (Node n : topo.getNodes())
        	handler.nodeAdded(n);
        topo.setProperty("popupRunning",false);
    }
   
    /**
     * Creates new form NewJPanel
     */
    public NewJPanel() {
        initComponents();
    }

    
     class EventHandler implements TopologyListener, MovementListener, ConnectivityListener,
			PropertyListener, MouseListener, ActionListener{
	    public void nodeAdded(Node n){
	    	JNode jv=new JNode(n);
	        n.setProperty("jnode", jv);
	        n.addPropertyListener(this);
	        add(jv);
	        updateUI();
	    }
	    public void nodeRemoved(Node n){
	    	JNode jn=(JNode)n.getProperty("jnode");
	        remove(jn);
	        updateUI();
	    }
	    public void linkAdded(Link l){
	    	l.addPropertyListener(this);
	    	updateUI();
	    }
	    public void linkRemoved(Link l){
	    	updateUI();
	    }	    
	    public void nodeMoved(Node n) {
	    	updateUI();
	    	((JNode)n.getProperty("jnode")).update();
	    }
		public void propertyChanged(Object o, String property){
	    	if (property.equals("color"))
	    		((JNode)((Node)o).getProperty("jnode")).updateUI();
	    	if (property.equals("id"))
	    		((JNode)((Node)o).getProperty("jnode")).setToolTipText(o.toString());
			if (property.equals("lineWidth"))
				updateUI();
		}		
	    public void mousePressed(MouseEvent e) {
	    	if ((Boolean)topo.getProperty("popupRunning")==true){
	    		topo.setProperty("popupRunning", false);
	    		return;
	    	}
	    	if (e.getButton()==MouseEvent.BUTTON1){
	        	JPopupMenu popup=new JPopupMenu();
	        	for (String type : Node.getModelsNames()){
	        		JMenuItem jitem=new JMenuItem(type);
	        		jitem.setActionCommand("addNode "+type+" "+e.getX()+" "+e.getY());
	        		jitem.addActionListener(this);
	        		popup.add(jitem);
	        	}
	        	if (popup.getComponentCount()>1){
	            	topo.setProperty("popupRunning", true);
	        		popup.show(jtopo,e.getX(),e.getY());
	        	}else{
	        		topo.addNode(e.getX(),e.getY(),Node.newInstanceOfModel("default"));
	        	}
	        }else if (e.getButton()==MouseEvent.BUTTON3){
	        	JPopupMenu popup=new JPopupMenu();
	        	for (String command : actionCommands){
	        		JMenuItem jitem=new JMenuItem(command);
	        		for (ActionListener al : actionListeners)
	        			jitem.addActionListener(al);
	        		jitem.addActionListener(this);
	        		popup.add(jitem);
	        	}
	        	topo.setProperty("popupRunning", true);
	    		popup.show(jtopo,e.getX(),e.getY());
	        }
	    }
	    
		public void actionPerformed(ActionEvent arg0) {
			topo.setProperty("popupRunning", false);
			String[] args=arg0.getActionCommand().split(" ");
			if (args[0].equals("addNode")){
				int x=Integer.parseInt(args[2]);
				int y=Integer.parseInt(args[3]);
				topo.addNode(x, y, Node.newInstanceOfModel(args[1]));
			}
		}
	    public void mouseClicked(MouseEvent e){}
	    public void mouseEntered(MouseEvent e){}
	    public void mouseExited(MouseEvent e){}
	    public void mouseReleased(MouseEvent e){}
	}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
